package com.ssh.controller;

import com.ssh.common.Result;
import com.ssh.entity.User;
import com.ssh.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController extends CommonController {
    Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping(value = "/api/v1/index")
    public String index(){
        return "Hello World!";
    }

    @ResponseBody
    @RequestMapping(value = "/api/v1/user/add", method = RequestMethod.POST)
    public Result addUser(User user) {
        if (user == null || user.getAccount() == null || user.getPwd() == null) {
            return Result.error("用户名或密码为空！");
        }
        return userService.addUser(user);
    }

    @ResponseBody
    @RequestMapping(value = "/api/v1/user/login", method = RequestMethod.POST)
    public Result login(User user) {
        if (user == null || user.getAccount() == null || user.getPwd() == null) {
            return Result.error("用户名或密码为空！");
        }
        return userService.login(user);
    }
}
